package com.cysoft.starter.log.core.jdbcsql.impl;

import com.cysoft.starter.log.core.jdbcsql.JdbcBase;

/**
 * @author 玖零
 * @version v0.0.1
 * @project cysoft-spring-boot-starter-log
 * @package com.cysoft.starter.log.jdbcsql
 * @date 2025-02-05 00:53
 * @desc TODO
 */
public class Mysql implements JdbcBase {


    /**
     * 创建表DDL语句
     */
    private String createTableDdl = "create table %s\n " +
            "(\n" +
            "  `id` bigint NOT NULL COMMENT '主键id',\n" +
            "  `log_type` varchar(20) DEFAULT NULL COMMENT '日志类型 login:登录日志 service:服务日志',\n" +
            "  `request_url` varchar(255) DEFAULT NULL COMMENT '请求地址',\n" +
            "  `request_ip` varchar(64) DEFAULT NULL COMMENT '请求ip',\n" +
            "  `request_header` longtext COMMENT '请求头信息，使用JSON存储',\n" +
            "  `request_body` longtext COMMENT '请求体，所有请求参数默认转换成JSON方式进行存储',\n" +
            "  `response_body` longtext COMMENT '响应体，所有相应参数默认转换成JSON方式进行存储',\n" +
            "  `request_result` bit(1) DEFAULT b'1' COMMENT '请求结果，1 成功 0 失败',\n" +
            "  `error_message` longtext COMMENT '异常信息',\n" +
            "  `create_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
            "  `duration` bigint DEFAULT '0' COMMENT '处理耗时，默认为0',\n" +
            "  `package_info` varchar(255) DEFAULT NULL COMMENT '包信息',\n" +
            "  `class_info` varchar(255) DEFAULT NULL COMMENT '类信息',\n" +
            "  `method_info` varchar(255) DEFAULT NULL COMMENT '方法信息',\n" +
            "  `operator` varchar(255) DEFAULT NULL COMMENT '操作人',\n" +
            "  `request_method` varchar(16) DEFAULT NULL COMMENT '请求方式',\n" +
            "  PRIMARY KEY (`id`)\n" +
            ") ENGINE=InnoDB  COMMENT='日志记录表'";

    /**
     * 校验数据库连接是否有效
     */
    private String checkDatabaseConnectionStatus = "select 1";

    /**
     * 校验表是否存在
     */
    private String checkTableExists = "select count(1) from information_schema.tables where table_name = '%s' and table_schema = '%s' limit 1";


    /**
     * 插入日志的SQL语句
     */
    private String insertData = "insert into %s (id, log_type, request_url, request_ip, request_header, request_body, response_body, request_result, error_message, create_time, duration, package_info, class_info, method_info, operator,request_method) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";


    /**
     * 重写获取创建表DDL语句
     *
     * @return
     */
    @Override
    public String getCreateTableDdl() {
        return createTableDdl;
    }


    @Override
    public String getCheckDatabaseConnectionStatus() {
        return checkDatabaseConnectionStatus;
    }


    @Override
    public String getCheckTableExists() {
        return checkTableExists;
    }

    @Override
    public String getInsertData() {
        return insertData;
    }
}
